﻿using System;
using System.Xml.Serialization;
using System.Management.Instrumentation;
using System.Collections.Generic;
using System.Diagnostics;

namespace Dynamic.Facades.EntLib.Logging
{
    /// <summary>
    /// 日志实体类
    /// </summary>
    [XmlRoot("logEntry")]
    [Serializable]
    [InstrumentationClass(InstrumentationType.Event)]
    [ManagedName("LogEntryV20")]
    public class LogEntry : Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry
    {
        private EventCategory _category;

        /// <summary>
        /// 构造函数
        /// </summary>
        public LogEntry()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        /// <param name="priority"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="title"></param>
        /// <param name="properties"></param>
        public LogEntry(object message, EventCategory category, int priority, int eventId,
                        TraceEventType severity, string title, IDictionary<string, object> properties)
            : this(message, BuildCategoriesCollection(category), priority, eventId, severity, title, properties)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        /// <param name="priority"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="title"></param>
        /// <param name="properties"></param>
        protected LogEntry(object message, string category, int priority, int eventId,
                       TraceEventType severity, string title, IDictionary<string, object> properties)
            : this(message, BuildCategoriesCollection(category), priority, eventId, severity, title, properties)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="categories"></param>
        /// <param name="priority"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="title"></param>
        /// <param name="properties"></param>
        protected LogEntry(object message, ICollection<string> categories, int priority, int eventId,
                        TraceEventType severity, string title, IDictionary<string, object> properties)
            : base(message, categories, priority, eventId, severity, title, properties)
        {
        }

        private static ICollection<string> BuildCategoriesCollection(EventCategory category)
        {
            if ((int)category == 0)
            {
                throw new ArgumentNullException("category");
            }

            return category.ToString().Split(',');
        }

        /// <summary>
        /// 日志类型
        /// </summary>
        public EventCategory Category
        {
            get
            {
                return _category;
            }
            set
            {
                _category = value;

                ICollection<string> categories = _category.ToString().Split(',');
                foreach (string item in categories)
                {
                    if (this.Categories.Contains(item.Trim()))
                    {
                        continue;
                    }
                    this.Categories.Add(item.Trim());
                }
            }
        }

        private static ICollection<string> BuildCategoriesCollection(string category)
        {
            if (string.IsNullOrEmpty(category))
                throw new ArgumentNullException("category");

            return new string[] { category };
        }
    }
}
